package org.skywind.Index.HitMappers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.skywind.Index.IndexManager;
import org.skywind.Robot.AudioMp3Robot;
import org.skywind.Robot.SpiderBase;
import org.skywind.WindSearch.Beans.Mp3Bean;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;

public class MapMP3 extends AbstractMapHits {

	public List mapping(MapEvent me) {
		Hits hits = me.getHits();
		Query query=me.getQuery();
		IndexManager im = me.getIndex();
		SpiderBase sb = me.getSpiderbase();
		List list = new ArrayList();
		try {
			for (int i = 0; i < hits.length(); i++) {
				Document doc;
				doc = hits.doc(i);
				Mp3Bean mp3 = new Mp3Bean();
				mp3.setTitle(highlight(query,AudioMp3Robot.TITLE,doc.get(AudioMp3Robot.TITLE), im.getAnalyzer()));
				mp3.setArtist(highlight(query,AudioMp3Robot.ARTIST,doc.get(AudioMp3Robot.ARTIST), im.getAnalyzer()));
				mp3.setSpecial(highlight(query,AudioMp3Robot.SPECIAL,doc.get(AudioMp3Robot.SPECIAL), im.getAnalyzer()));
				mp3.setSize(Long.parseLong(doc.get(AudioMp3Robot.SIZE)));
				mp3.setDate(Long.parseLong(doc.get(AudioMp3Robot.DATE)));
				mp3.setUrl(doc.get(AudioMp3Robot.URL));
				mp3.setType(doc.get(AudioMp3Robot.TYPE));
				mp3.setSpeed(Integer.parseInt(doc.get(AudioMp3Robot.SPEED)));
				mp3.setTime(new Date(Long.parseLong(doc.get(AudioMp3Robot.TIME))));
				mp3.setSourceid(hits.id(i));
				mp3.setSourceIP(sb.getRobotManager().getDataHost());
				mp3.setRank((double)i/hits.length());
				list.add(mp3);
				if (maxResult >0 && i >= maxResult)
					break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

}
